package io.itit.ecp.admin.provide.action.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.itit.ecp.admin.common.constants.ParamConstants;
import io.itit.ecp.admin.common.enums.StatisticsModeEnums;
import io.itit.ecp.admin.provide.action.req.ProjectMeterStatisticQueryReq;
import io.itit.ecp.admin.provide.action.resp.ProjectMeterStatisticsResp;
import io.itit.ecp.admin.provide.filter.IEnergyDataFilter;
import io.itit.ecp.admin.server.entity.extend.EnergyDataExtEntity;
import io.itit.ecp.admin.server.service.IEnergyDataService;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IExport;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.common.provide.utils.ParamUtils;
import io.itit.grass.common.server.dto.ParamDto;
import io.itit.grass.common.server.utils.ConvertUtils;
import io.itit.grass.estelle.starter.core.ExcelXSSFUtil;
import io.itit.grass.estelle.starter.util.JsonUtil;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：项目计量统计控制类
 * @Date：Create by 2023-12-05 11:48:22
 */
@Api(tags = "项目计量统计")
@RestController
@RequestMapping("/ecp-admin/projectMeterStatistic")
public class ProjectMeterStatisticController {

    @Resource
    private IEnergyDataService energyDataService;

    @ApiOperation("项目计量统计分页查询")
    @PostMapping("queryPage")
    @RequiresPermissions("ecp-admin:projectMeterStatistic:search")
    public ResultResp<IPage<ProjectMeterStatisticsResp>> queryPage(@RequestBody @Validated(IQueryPage.class) ProjectMeterStatisticQueryReq req) {
        ParamDto paramDto = ParamUtils.paramDto(req);
        if (!"hour".equals(req.getStatisticsMode())) {
            paramDto.add(ParamConstants.MYSQL_DATE_FORMAT, StatisticsModeEnums.mysqlDateFormat(req.getStatisticsMode()));
        } else {
            paramDto.add(ParamConstants.MYSQL_DATE_FORMAT, "%Y-%m-%d %H:%m");
            if (!paramDto.containsKey("solidifyDateStart")) {//默认前1小时数据
                paramDto.add("solidifyDateStart", LocalDateTime.now().minusHours(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }
        }
        if ("day".equals(req.getStatisticsMode()) && !paramDto.containsKey("solidifyDateStart")) {
            paramDto.add("solidifyDateStart", LocalDate.now().atTime(LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
        IPage<ProjectMeterStatisticsResp> page = ConvertUtils.convert(energyDataService.projectMeterStatistics(paramDto,
                PageUtils.getPage(req, EnergyDataExtEntity.class)), ProjectMeterStatisticsResp.class);
        return ResultResp.success(page);
    }

    @ApiOperation("导出")
    @PostMapping("export")
    @RequiresPermissions("ecp-admin:energyData:export")
    public void export(@RequestBody @Validated(IExport.class) ProjectMeterStatisticQueryReq req, HttpServletResponse response) {
        List<EnergyDataExtEntity> list = energyDataService.projecMeterExport(ParamUtils.paramDto(req)
                .add(ParamConstants.MYSQL_DATE_FORMAT, StatisticsModeEnums.mysqlDateFormat(req.getStatisticsMode())));
        ExcelXSSFUtil.export("ProjectMeterStatisticsExport", JsonUtil.toJson(list, new IEnergyDataFilter.EnergyDataExtExportFilter()), response);
    }


}
